<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\services\user;

use app\services\BaseServices;
use app\dao\user\UserRepairSignDao;
use crmeb\exceptions\AdminException;
use crmeb\exceptions\ApiException;

/**
 *
 * Class UserSignServices
 * @package app\services\user
 */
class UserRepairSignServices extends BaseServices
{

    /**
     * UserSignServices constructor.
     * @param UserRepairSignDao $dao
     */
    public function __construct(UserRepairSignDao $dao)
    {
        $this->dao = $dao;
    }

    /**
     * 获取补签卡
     */
    public function getRepairSign($uid){
        /** @var UserServices $userServices */
        $userServices = app()->make(UserServices::class);
        $user = $userServices->getUserInfo($uid);
        if($user->integral < 8){
            throw new AdminException(100006);
        }

        $where = [
            'uid' => $uid,
        ];
        $dateline = time() - (86400 * 14);
        $num = $this->dao->getRepairSignNum($where, $dateline);
        if($num >= 2){
            throw new AdminException(100006);
        }

        $data = [
            'uid' => $uid,
            'add_time' => time(),
        ];
        $this->dao->save($data);

        $user->integral = $user->integral - 8;
        $user->repair_sign_num = $user->repair_sign_num + 1;
        if (!$user->save()) {
            throw new ApiException(100006);
        }

        $bill = [
            'uid' => $uid,
            'title' => '兑换补签卡',
            'number' => 0,
            'time' => date("Y-m-d"),
            'balance' => $user->integral,
        ];
        $userBill = app()->make(UserBillServices::class);
        $bill['mark'] = $bill['title'];
        if (!$userBill->expendIntegral($uid, 'get_repair', $bill)) {
            throw new ApiException(100006);
        }

        return 100000;
    }

    /**
     * @param $uid
     * @param $date
     * @throws \ReflectionException
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function repairSign($uid, $date){
        /** @var UserServices $userServices */
        $userServices = app()->make(UserServices::class);
        $user = $userServices->getUserInfo($uid);
        if($user->repair_sign_num <= 0){
            throw new AdminException(100006);
        }

        /** @var UserSignServices $userSignServices */
        $userSignServices = app()->make(UserSignServices::class);
        $num = $userSignServices->getUserSignByDate($uid, $date);

        if($num > 0){
            throw new ApiException(100006);
        }

        if (!$userSignServices->setSignData($uid, '补签奖励',2,$user->integral,$date)) {
            throw new ApiException(100006);
        }

        $user->integral = $user->integral + 2;
        $user->repair_sign_num = $user->repair_sign_num - 1;
        if (!$user->save()) {
            throw new ApiException(100006);
        }

        return 100000;
    }
}
